package com.ruoyi.event.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.event.domain.EventYwwczl;
import com.ruoyi.event.domain.bo.EventYwwczlBo;
import com.ruoyi.event.domain.vo.EventYwwczlVo;
import com.ruoyi.event.mapper.EventYwwczlMapper;
import com.ruoyi.event.service.IEventYwwczlService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 药物维持治疗Service业务层处理
 *
 * @author strongme
 * @date 2023-02-04
 */
@RequiredArgsConstructor
@Service
public class EventYwwczlServiceImpl implements IEventYwwczlService {

    private final EventYwwczlMapper baseMapper;

    /**
     * 查询药物维持治疗
     */
    @Override
    public EventYwwczlVo queryById(Long ywwczlId) {
        return baseMapper.selectVoById(ywwczlId);
    }

    /**
     * 查询药物维持治疗列表
     */
    @Override
    public TableDataInfo<EventYwwczlVo> queryPageList(EventYwwczlBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<EventYwwczl> lqw = buildQueryWrapper(bo);
        Page<EventYwwczlVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询药物维持治疗列表
     */
    @Override
    public List<EventYwwczlVo> queryList(EventYwwczlBo bo) {
        LambdaQueryWrapper<EventYwwczl> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<EventYwwczl> buildQueryWrapper(EventYwwczlBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<EventYwwczl> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, EventYwwczl::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getYw()), EventYwwczl::getYw, bo.getYw());
        lqw.eq(StringUtils.isNotBlank(bo.getZzys()), EventYwwczl::getZzys, bo.getZzys());
        lqw.eq(bo.getCcfysj() != null, EventYwwczl::getCcfysj, bo.getCcfysj());
        lqw.eq(bo.getZlrq() != null, EventYwwczl::getZlrq, bo.getZlrq());
        lqw.eq(bo.getFycs() != null, EventYwwczl::getFycs, bo.getFycs());
        lqw.eq(StringUtils.isNotBlank(bo.getFyl()), EventYwwczl::getFyl, bo.getFyl());
        lqw.eq(StringUtils.isNotBlank(bo.getZldq()), EventYwwczl::getZldq, bo.getZldq());
        lqw.eq(StringUtils.isNotBlank(bo.getZldw()), EventYwwczl::getZldw, bo.getZldw());
        return lqw;
    }

    /**
     * 新增药物维持治疗
     */
    @Override
    public Boolean insertByBo(EventYwwczlBo bo) {
        EventYwwczl add = BeanUtil.toBean(bo, EventYwwczl.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setYwwczlId(add.getYwwczlId());
        }
        return flag;
    }

    /**
     * 修改药物维持治疗
     */
    @Override
    public Boolean updateByBo(EventYwwczlBo bo) {
        EventYwwczl update = BeanUtil.toBean(bo, EventYwwczl.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(EventYwwczl entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除药物维持治疗
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
